﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

using OgilvyOne.PreorderWap;
public partial class RegInfo : System.Web.UI.Page
{
    protected string source = "";
    protected string channel = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["source"]))
                source = Request.QueryString["source"];
            if (!string.IsNullOrEmpty(Request.QueryString["channel"]))
                channel = Request.QueryString["channel"];

            if (string.IsNullOrEmpty(Request.QueryString["model"]) && string.IsNullOrEmpty(Request.QueryString["capcity"]) && string.IsNullOrEmpty(Request.QueryString["color"]))
            {
                Response.Redirect("Default.aspx");
            }

            //预览返回
            if (!string.IsNullOrEmpty(Request.QueryString["name"]))
            {
                this.input_name.Text = Request.QueryString["name"];
                if (Request.QueryString["sex"] == "先生")
                {
                    this.input_male.Checked = true;
                }
                else
                {
                    this.input_female.Checked = true;
                }
                this.input_number.Text = Request.QueryString["mobile"];
                this.input_email.Text = Request.QueryString["email"];
                this.input_budgetfrom.Text = Request.QueryString["budgetFrom"];
                this.input_budgetto.Text = Request.QueryString["budgetTo"];
                this.input_year.Items.FindByText("请选择").Selected = false;
                this.input_year.Items.FindByText(Request.QueryString["year"]).Selected = true;
                this.input_month.Items.FindByText("请选择").Selected = false;
                this.input_month.Items.FindByText(Request.QueryString["month"]).Selected = true;
            }
        }
    }
    protected void Sub_Click(object sender, EventArgs e)
    {
        string name = this.input_name.Text;
        string sex = this.input_male.Checked == true ? "先生" : "女士";
        string mobile = this.input_number.Text;
        string email = this.input_email.Text;
        string budget = this.input_budgetfrom.Text + "万元-" + this.input_budgetto.Text + "万元";
        string mention = this.input_year.SelectedItem.Text  + this.input_month.SelectedItem.Text + "月";
        string model = Request.QueryString["model"];
        string capcity = Request.QueryString["capcity"];
        string color = Request.QueryString["color"];
        string seat = Request.QueryString["seat"];
        string resultInfo=Check();

        //正式
        string urlAddress = "http://vico1.vw.com.cn/newcms/testdrive/api/regsave.aspx";
        //测试
        //string urlAddress = "http://vico.ogilvy.com.cn/newcms/testdrive/api/regsave.aspx";
        if (resultInfo != "")
        {
            Response.Write("<script>alert('" + resultInfo + "');</script>");
        }
        else
        {
            try
            {
                int flag = ADD_Wap(name, sex, mobile, email, budget, mention, capcity, model, color, seat,Request.QueryString["source"],Request.QueryString["channel"]);
                
                //sendmail
                MailService mail = new MailService();
                string htmlMail = mail.generateEmail(color + "_mail.png", "email", name, sex, mobile, email, budget, mention, capcity, model, converColor(color), converSeat(seat));
                mail.sendMail(email, htmlMail);
                //api
                PostAPI post = new PostAPI();
                string strSeat=string.Empty;
                if (color == "jiaoshilan")
                {
                    if (capcity == "1.4TSI")
                    {
                        if (model == "时尚型")
                        {
                            strSeat = "";
                        }
                    }
                }
                else
                {
                    strSeat = converSeat(seat);
                }
                string result = post.Save(name, sex, mobile, email, mention, budget, Request.QueryString["source"], capcity, model, converColor(color), strSeat, Request.QueryString["channel"], urlAddress);
            }
            catch(Exception ex)
            {
                Response.Redirect("success.aspx?color=" + color + "&utm_source=" + Request.QueryString["source"] + "&utm_medium=" + Request.QueryString["channel"]);
            }
            Response.Redirect("success.aspx?color=" + color + "&utm_source=" + Request.QueryString["source"] + "&utm_medium=" + Request.QueryString["channel"]);
        }

        
    }
    protected void Preview_Click(object sender, EventArgs e)
    {
        string resultInfo = Check();
        if (resultInfo != "")
        {
            Response.Write("<script>alert('" + resultInfo + "');</script>");
        }
        else
        {
            string name = this.input_name.Text;
            string sex = this.input_male.Checked == true ? "先生" : "女士";
            string mobile = this.input_number.Text;
            string email = this.input_email.Text;
            string budgetFrom = this.input_budgetfrom.Text;
            string budgetTo = this.input_budgetto.Text;
            string year = this.input_year.SelectedItem.Text;
            string month = this.input_month.SelectedItem.Text;
            string model = Request.QueryString["model"];
            string capcity = Request.QueryString["capcity"];
            string color = Request.QueryString["color"];
            string seat = Request.QueryString["seat"];
            string param = string.Format("&name={0}&sex={1}&mobile={2}&email={3}&budgetFrom={4}&budgetTo={5}&year={6}&month={7}&capcity={8}&model={9}&color={10}&seat={11}&source={12}&channel={13}&utm_source={14}&utm_medium={15}", name, sex, mobile, email, budgetFrom, budgetTo, year, month, capcity, model, color, seat, Request.QueryString["source"], Request.QueryString["channel"], Request.QueryString["source"], Request.QueryString["channel"]);
            Response.Redirect("preview.aspx?" + param);
        }
    }

    private int ADD_Wap(string name, string sex, string mobile, string email, string budget, string mention, string capcity, string carModel, string carColor, string carSeat,string source,string channel)
    {
        PreorderWapDAL use = new PreorderWapDAL();
        int resule = use.InsertWap(name, sex, mobile, email, budget, mention, capcity, carModel, converColor(carColor), converSeat(carSeat),source,channel);
        return resule;
    }

    public string converColor(string color)
    {
        string flag = "";
        switch (color)
        {
            case"jiaoshilan":
                flag = "金属漆_礁石蓝";
                break;
            case "lingyangbai":
                flag = "珠光漆_羚羊白";
                break;
            case "jufenghong":
                flag = "标准漆_飓风红";
                break;
            case "niuzailan":
                flag = "标准漆_牛仔蓝";
                break;
            case "tuxinghuang":
                flag = "标准漆_土星黄";
                break;
            case "xuanmuyin":
                flag = "金属漆_眩目银";
                break;
            case "tangguobai":
                flag = "标准漆_糖果白";
                break;
            case "zhenghei":
                flag = "标准漆_正黑";
                break;
            case "taifeihe":
                flag = "金属漆_太妃褐";
                break;
            case "shenhei":
                flag = "珠光漆_深黑";
                break;
            case "bojinhui":
                flag = "金属漆_铂金灰";
                break;
        }
        return flag;
    }

    public string converSeat(string seat)
    {
        string flag = "";
        switch (seat)
        {
            case "chunhei":
                flag = "真皮座椅_纯黑";
                break;
            case "mise":
                flag = "真皮座椅_米色";
                break;
            case "heilan":
                flag = "真皮座椅_黑色-蓝色(双色)";
                break;
            case "honghei":
                flag = "真皮座椅_红色-黑色(双色)";
                break;
            case "heise":
                flag = "织物座椅_黑色";
                break;
            case "shenmise":
                flag = "织物座椅_深米色";
                break;
        }
        return flag;
    }

    private string Check()
    {
        string error="";

        if (string.IsNullOrEmpty(this.input_name.Text))
        {
            error += "请输入姓名\\n";
        }
        else
        {
            string[] num = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            foreach(string s in num)
            {
                if (this.input_name.Text.IndexOf(s) >= 0)
                {
                    error = "请输入真实姓名\\n";
                    break;
                }
            }
            
        }
            
        if (this.input_male.Checked == false && this.input_female.Checked == false)
            error += "请选择性别\\n";
        if (string.IsNullOrEmpty(this.input_number.Text))
        {
            error += "请输入手机号\\n";
        }
        else
        {
            Regex ReMobile = new Regex(@"^1[358]\d{9}$");
            if (!ReMobile.IsMatch(this.input_number.Text))
            {
                error += "手机号应为11位有效数字\\n";
            }
        }
        if (string.IsNullOrEmpty(this.input_email.Text))
        {
            error += "请输入邮箱地址\\n";
        }
        else
        {
            Regex RegEmail = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
            if (!RegEmail.IsMatch(this.input_email.Text))
            {
                error += "邮件地址格式不正确,请重新输入\\n";
            }
            else
            {
                int flag = IsEmail(this.input_email.Text);
                if (flag > 0)
                    error += "此邮箱地址已成功预定甲壳虫，请保持手机畅通，我们客服人员会跟您确认详细信息！\\n";
            }
        }

        if (string.IsNullOrEmpty(this.input_budgetfrom.Text) || string.IsNullOrEmpty(this.input_budgetto.Text))
        {
            error += "请输入您的购车预算\\n";
        }
        else
        {
            Regex RegNumber = new Regex("^[0-9]+$");
            if (!RegNumber.IsMatch(this.input_budgetfrom.Text) || !RegNumber.IsMatch(this.input_budgetto.Text))
            {
                error += "购车预算应为整数\\n";
            }
        }

        if (this.input_year.SelectedItem.Text == "请选择" || this.input_month.SelectedItem.Text == "请选择")
        {
            error += "请选择购车时间\\n";
        }

        return error;
    }

    private int IsEmail(string email)
    {
        PreorderWapDAL use = new PreorderWapDAL();
        return use.IsEmail(email);
    }
}